package pl.gigiel.behaviour.steer.factory;

import java.lang.reflect.Method;

import pl.gigiel.behaviour.ISteer;
import pl.gigiel.behaviour.ISteerFactory;
import pl.gigiel.behaviour.exception.BehaviourException;


public class SteerFactory implements ISteerFactory {
	private static ISteerFactory steerFactory;

	private SteerFactory() {
	}

	public static ISteerFactory getInstance() {
		if (steerFactory == null) {
			steerFactory = new SteerFactory();
		}
		return steerFactory;
	}

	@Override
	public <T extends ISteer> ISteer createSteer(Class<T> steerClass) throws BehaviourException {
		ISteer steer = null;
		try {
			Method method = steerClass.getDeclaredMethod("getInstance");
			steer = (ISteer) method.invoke(null);
		} catch (Exception e) {
			throw new BehaviourException(e);
		}
		steer.init();
		return steer;
	}
}
